package com.test.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.test.bean.QuartzTask;

@Repository("quartzTaskDao")
public class QuartzTaskDao {

	@Autowired
	private DataSource dataSource;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	/**
	 * 查询
	 * @param QuartzTask
	 * @return
	 */
	public QuartzTask getQuartzTaskById(final QuartzTask quartzTask){
		String sql="select * from QUARTZ_TASK where QUARTZ_ID=?";
	    jdbcTemplate.query(sql, new Object[]{quartzTask.getQuartz_id()},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
			                                 quartzTask.setQuartz_name(rs.getString("QUARTZ_NAME"));
			                                 quartzTask.setQuartz_ano_name(rs.getString("QUARTZ_ANO_NAME"));
			                                 quartzTask.setQuartz_class_path(rs.getString("QUARTZ_CLASS_PATH"));
			                                 quartzTask.setQuartz_corn(rs.getString("QUARTZ_CORN"));
			                                 quartzTask.setQuartz_create_time(new Date(rs.getDate("QUARTZ_CREATE_TIME").getTime()));
			                                 quartzTask.setQuartz_abs(rs.getString("QUARTZ_ABS"));
			                                 quartzTask.setQuartz_type(rs.getString("QUARTZ_TYPE"));
			                                 quartzTask.setQuartz_id(rs.getString("QUARTZ_ID"));
				                             //task.setQuartz_abs(rs.getString("quartz_abs"));
				                        }          
		});  
		
		return quartzTask;
	}
	
	/**
	 * 添加
	 * @param QuartzTask
	 * @return
	 */
	public int add(QuartzTask quartzTask){
		String sql="insert into QUARTZ_TASK(QUARTZ_NAME, QUARTZ_ANO_NAME, QUARTZ_CLASS_PATH, QUARTZ_CORN, QUARTZ_CREATE_TIME, QUARTZ_ABS, QUARTZ_TYPE, QUARTZ_ID ) values(?, ?, ?, ?, ?, ?, ?, ? )";
		return jdbcTemplate.update(sql, new Object[]{quartzTask.getQuartz_name(),quartzTask.getQuartz_ano_name(),quartzTask.getQuartz_class_path(),quartzTask.getQuartz_corn(),quartzTask.getQuartz_create_time(),quartzTask.getQuartz_abs(),quartzTask.getQuartz_type(),"QUARTZ_TASK_SEQ.nextval"});
	}
	
	/**
	 * 更新
	 * @param QuartzTask
	 * @return
	 */
	public int updateQuartzTask(QuartzTask quartzTask){
		String sql="update QUARTZ_TASK set QUARTZ_NAME=?, QUARTZ_ANO_NAME=?, QUARTZ_CLASS_PATH=?, QUARTZ_CORN=?, QUARTZ_CREATE_TIME=?, QUARTZ_ABS=?, QUARTZ_TYPE=?,   where QUARTZ_ID=? ";
		return jdbcTemplate.update(sql, new Object[]{quartzTask.getQuartz_name(),quartzTask.getQuartz_ano_name(),quartzTask.getQuartz_class_path(),quartzTask.getQuartz_corn(),quartzTask.getQuartz_create_time(),quartzTask.getQuartz_abs(),quartzTask.getQuartz_type(),},quartzTask.getQuartz_id());
	}
	
	/**
	 * 删除 QuartzTask
	 * @param task
	 * @return
	 */
	public int delQuartzTask(QuartzTask quartzTask){
		String sql="delete QUARTZ_TASK  where QUARTZ_ID=?";
		return jdbcTemplate.update(sql, new Object[]{quartzTask.getQuartz_id()});
	}
	
	/**
	 * 获取所有 QuartzTask
	 * @return
	 */
	public List<QuartzTask>getAllQuartzTask(){
		String sql="select * from QUARTZ_TASK";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(QuartzTask.class));
	}
	
	
	/**
	 * 获取所有分页任务
	 * @return
	 */
	public List<QuartzTask>getPageAllQuartzTask(String sql){
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(QuartzTask.class));
	}
	
	
	/**
	 * 分页获取页数 
	 */
	public String getCountPage(String sql){
		String result=jdbcTemplate.queryForObject(sql,String.class);
		return result;
	}
	
}
